package android.Spider;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MessageTopic extends ListActivity
{
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

        m_dlgProgress = ProgressDialog.show(this, "Loading threads", "Please wait while loading...", true, true);

        m_connection.Initialize(this);
        m_idTopic = getIntent().getExtras().getInt(MessageBoard.s_strTopic);

		setListAdapter(m_listAdapter);
	}

	@Override
	protected void onDestroy() {
		m_connection.Unregister();
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    
	    menu.add(0, 0, "Create a thread", R.drawable.createthread);
	    menu.add(0, 1, "Refresh", R.drawable.refresh);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item)
	{
	    if (item.getId() == 0)
	    {
	    	Intent intent = new Intent(this, ThreadCreate.class);
			intent.putExtra(MessageBoard.s_strTopic, m_idTopic);
			startSubActivity(intent, 0);
	    	return true;
	    }
	    else if (item.getId() == 1)
	    {
	        m_listAdapter.Clear();

//	        if (m_dlgProgress == null)
//				m_dlgProgress = ProgressDialog.show(this, "Loading threads", "Please wait while loading...", true, true);
	    	
			m_connection.RequestThreads(m_idTopic);

			return true;
	    }
	    
	    return false;
	}

    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
    	Intent intent = new Intent(this, ThreadDisplay.class);
    	Bundle bundle = m_listAdapter.m_lstThreads.get(position).toBundle();
    	intent.putExtras(bundle);
    	startSubActivity(intent, 0);
		super.onListItemClick(l, v, position, id);
	}

	class TopicThreadsAdapter extends BaseAdapter
    {
    	private ArrayList<TopicThread> m_lstThreads = new ArrayList<TopicThread>();
    	
    	void AddThread(TopicThread thrd) { m_lstThreads.add(thrd); this.notifyDataSetChanged(); }
    	void AddThreads(ArrayList<TopicThread> lstThreads) { m_lstThreads = lstThreads; this.notifyDataSetChanged(); }
    	void Clear() { m_lstThreads.clear(); this.notifyDataSetChanged(); }
    	
        public int getCount() { return m_lstThreads.size(); }

        public Object getItem(int position) { return position; }

        public long getItemId(int position) { return position; }

        public View getView(int position, View convertView, ViewGroup parent)
        {
        	if (convertView == null)
        		convertView = getViewInflate().inflate(R.layout.messagethreaditem, null, null);

        	// Thread title
        	TopicThread thrd = m_lstThreads.get(position);
        	TextView tv = (TextView)convertView.findViewById(R.id.ThreadTitle);
        	tv.setText(thrd.m_strThreadName);

        	// Short description
        	tv = (TextView)convertView.findViewById(R.id.ThreadShortText);
        	TopicThread.Issuer issuer = thrd.m_lstPosts.get(0);
        	tv.setMaxLines(3);
        	tv.setText(issuer.m_strText);
        	
        	// Created by Name
        	tv = (TextView)convertView.findViewById(R.id.ThreadByName);
        	int nPos = issuer.m_strName.indexOf('@');
        	String str = issuer.m_strName.substring(0, nPos);
        	tv.setText(" " + str);
        	
        	// Created by Date
        	tv = (TextView)convertView.findViewById(R.id.ThreadByDate);
        	tv.setText(" " + issuer.m_strTime);
        	
            // Last added by Name
            tv = (TextView)convertView.findViewById(R.id.ThreadLastName);
            issuer = thrd.m_lstPosts.get(thrd.m_lstPosts.size() - 1);
        	nPos = issuer.m_strName.indexOf('@');
        	str = issuer.m_strName.substring(0, nPos);
        	tv.setText(" " + str);

            // Last added by Date
            tv = (TextView)convertView.findViewById(R.id.ThreadLastDate);
            tv.setText(" " + issuer.m_strTime);	
            		
        	return convertView;
        }
    };
    
    private int m_idTopic;
    private TopicThreadsAdapter m_listAdapter = new TopicThreadsAdapter();

    private XmppConnection m_connection = new XmppConnection()
    {

		@Override
		protected void OnConnected()
		{
			// Initialize downloading of all threads
			RequestThreads(m_idTopic);
		}

		@Override
		protected void OnThreadsReceived(ArrayList<TopicThread> lstThreads)
		{
			m_listAdapter.AddThreads(lstThreads);
			if (m_dlgProgress != null)
			{
				m_dlgProgress.cancel();
				m_dlgProgress = null;
			}
		}

    };

    ProgressDialog m_dlgProgress = null;
}
